$(document).ready(function() {
        var plan_id = document.plan_id;
        var media_url = document.media_url;

        $(".course_ac").autocomplete(courses,
            {
                minChars: 1,
                max: 5,
                width: 450,
                matchContains: "word",
                autoFill: false,
                formatItem: function(row, i, max) {
                    return "\"" + row.name + "\" [" + row.code + "]";
                },
                formatMatch: function(row, i, max) {
                    return row.name + " " + row.code;
                },
                formatResult: function(row) {
                    return row.code;
                }
            });

        $("#Plan form").bind('submit', function() {
            var options = {
                // type: 'post',
                // dataType: 'json',
                success: addCourseToTerm
                //url: '/plans/' + plan_id + '/planned_courses/new/',
            };

            $(this).ajaxSubmit(options);
            return false;
        });


        $(".dropCourse").live('click', function () {
            var table_row = $(this).parents('tr:first');
            var course_id_input = $('.planned_course_id', table_row);
            var planned_course_id = course_id_input.val();


            $.ajax({
                type: "POST",
                // FIXME relative path hack?
                // would fail if ever actually at /plans/<plan_id>/ instead of /plans/
                // right now we need relative because absolute paths fail when at a relative
                // server path
                error: function(xhr, textStatus, errorThrown) {
                    alert(xhr.responseText);
                },
                url: plan_id + "/planned_courses/" + planned_course_id + "/delete/",
                success: function(){
                    $(table_row).fadeOut('medium');
                }
            });

            validatePlan();
            return false;
        });

        function dropCourse(obj, id) {
            obj.log();
            //#console.log(id);
            return false;
        }

        function addCourseToTerm(responseObj, statusText) {
            // var pc_id = responseObj.planned_course_id;
            // var course = responseObj.course;
            var term = $('.planned_course_term', responseObj).val();
            var table = $("#" + term);
            // tb_init('a.thickbox', responseObj);
            // var table_row = "<tr>";
            // table_row += "<input type=\"hidden\" class=\"planned_course_id\" value=\"" + pc_id +"\"/>";
            // table_row += "<td>" + course.short_form + "</td>";
            // table_row += "<td>" + course.full_name + "</td>";
            // table_row += "<td>" + course.te_math + "</td>";
            // table_row += "<td>" + course.te_sci + "</td>";
            // table_row += "<td>" + course.te_engsci + "</td>";
            // table_row += "<td>" + course.te_engdes + "</td>";
            // table_row += "<td>" + course.cse + "</td>";
            // table_row += "<td><a class=\"dropCourse\" href=\"#\"><img src=\""+ media_url + "img/icons/delete.png\" /> Remove</a></td>";
            // table_row += "</tr>";
            // table.append(responseObj);
            r_obj = $(responseObj).appendTo(table);
            tb_init('a.thickbox', r_obj);


            validatePlan();
        };

        function validatePlan() {
                // FIXME relative path hack?
                // would fail if ever actually at /plans/<plan_id>/ instead of /plans/
                // right now we need relative because absolute paths fail when at a relative
                // server path
            $("#ValidationResults").load(plan_id + "/validate/");
        }

});

